using System;

namespace TXK
{
    /// <summary>
    /// 单个字节转换库
    /// </summary>
    public class ByteLib
    {
        /// <summary>
        /// 从字节数组中截取某个字节
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static byte GetByteFromByteArray(byte[] source, int start)
        {
            byte[] byteArray = ByteArrayLib.GetByteArray(source, start, 1);
            return (byte)((byteArray != null) ? byteArray[0] : 0);
        }

        /// <summary>
        /// 将字节中的某个位赋值
        /// </summary>
        /// <param name="value">原始字节</param>
        /// <param name="bit">位</param>
        /// <param name="val">写入数值</param>
        /// <returns>返回字节</returns>
        public static byte SetbitValue(byte value, int bit, bool val)
        {
            return val ? ((byte)(value | (byte)Math.Pow(2.0, bit))) : ((byte)(value & (byte)(~(byte)Math.Pow(2.0, bit))));
        }
    }
}
